package com.example.zhu95.mytimber.receiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;

import com.example.zhu95.mytimber.activity.BaseActivity;

import static com.example.zhu95.mytimber.ConstantValue.CURRENT_POSITION;
import static com.example.zhu95.mytimber.ConstantValue.IS_PROGRESSING;
import static com.example.zhu95.mytimber.ConstantValue.MUSIC_STATE;
import static com.example.zhu95.mytimber.ConstantValue.MusicState_Playing;
import static com.example.zhu95.mytimber.ConstantValue.SONG_ID;

/**
 * @author zhu95
 */
public class MusicStateReceiver extends BroadcastReceiver {

    BaseActivity activity;

    public void setActivity(BaseActivity activity) {
        this.activity = activity;
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        int songid = intent.getIntExtra(SONG_ID, -1);
        String musicState = intent.getStringExtra(MUSIC_STATE);
        int currentPosition = intent.getIntExtra(CURRENT_POSITION, -1);
        boolean isProgressing = intent.getBooleanExtra(IS_PROGRESSING, false);
        if (activity.isNotifyUpdateUI()) {
            activity.updateUI(songid, musicState, currentPosition);
            activity.setNotifyUpdateUI(false);
        }
        if (isProgressing && TextUtils.equals(musicState, MusicState_Playing)) {
            activity.updateProgress(songid, currentPosition);
        } else {
            activity.updateUI(songid, musicState, currentPosition);
        }
    }
}
